﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AddRankingUnits : MonoBehaviour
{
    public RankingCell rankingCellPrefab;
    public List<RankingInfo> rankingList = new List<RankingInfo>();
    public ArrayList rankingCellList = new ArrayList();
    public int currentPageIndex = 1;
    public GameObject moreRankingButton;

	// Use this for initialization
	void Start () {
        LoadingWindow.window.ShowShortLoading();
        RestAPI.singleton.GetUserRankingList(ReturnCallback, currentPageIndex++);
	}
	
	// Update is called once per frame
	void Update () {
	    
	}

    void SetCells()
    {
        for (int i = 0; i < rankingList.Count; i++)
        {
            if (rankingCellList.Count < i+1)
            {
				RankingCell cell = LTPrefabManager.InstantiateC(PrefabTypes.RankBar, Vector3.zero, Quaternion.identity) as RankingCell;
                cell.transform.parent = transform;
                cell.transform.localScale = new Vector3(1f, 1f, 1f);
//                cell.rankText.text = (i+1).ToString() + "위";
				if( i+1 > 9 )
				{
					cell.rankTextImage[1].spriteName = "No_A" + ((i+1) / 10);
					cell.rankTextImage[2].spriteName = "No_A" + ((i+1) % 10);
					cell.rankTextImage[1].gameObject.SetActive(true);
					cell.rankTextImage[2].gameObject.SetActive(true);
				}
				else if ( i+1 > 99 )
				{
					cell.rankTextImage[0].spriteName = "No_A" + ((i+1) / 100);
					cell.rankTextImage[1].spriteName = "No_A" + (((i+1) & 100) / 10);
					cell.rankTextImage[2].spriteName = "No_A" + ((i+1) % 10);
					cell.rankTextImage[0].gameObject.SetActive(true);
					cell.rankTextImage[1].gameObject.SetActive(true);
					cell.rankTextImage[2].gameObject.SetActive(true);
				}
				else
				{
					cell.rankTextImage[2].spriteName = "No_A" + ((i+1) % 10);
					cell.rankTextImage[2].gameObject.SetActive(true);
				}
                cell.userNameText.text = rankingList[i].userName.Trim();
                cell.pointText.text = rankingList[i].point.ToString();
				cell.userImage.spriteName = string.Format("IngameUi_cha{0:00}", rankingList[i].pawn);
//                cell.battleRecordText.text = string.Format("{0}전 {1}승 {2}탈락", rankingList[i].totalBattleCount, rankingList[i].winCount, rankingList[i].retireCount);
				if( rankingList[i].userId != MyInfo.Instance.DBID )
				{
					cell.bg.spriteName = "ListBox_B002";
				}
                rankingCellList.Add(cell);
            }
        }

        GetComponent<UIGrid>().Reposition();
    }

    void GetMoreRankingButtonCliked()
    {
        LoadingWindow.window.ShowShortLoading();
        RestAPI.singleton.GetUserRankingList(ReturnCallback, currentPageIndex++);
    }

    void ReturnCallback(Hashtable returnData)
    {
        LoadingWindow.window.HideLoading();
        if (returnData["list"] != null)
        {
            foreach (RankingInfo info in returnData["list"] as List<RankingInfo>)
            {
                rankingList.Add(info);
            }
            SetCells();
        }
        else
            moreRankingButton.SetActive(false);
    }
}
